﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace eStock.Common
{
    static class Functions
    {
        public static BitmapImage OpenImageFromFile(string filepath)
        {
                BitmapImage image = new BitmapImage();
                using (FileStream stream = File.OpenRead(filepath))
                {
                    image.BeginInit();
                    image.StreamSource = stream;
                    image.CacheOption = BitmapCacheOption.OnLoad;
                    image.EndInit(); // load the image from the stream
                } // close the stream
                return image;
        }
        static internal ImageSource GetImageFromResource(string psAssemblyName, string psResourceName)
        {
            Uri oUri = new Uri("pack://application:,,,/" + psAssemblyName + ";component/" + psResourceName, UriKind.RelativeOrAbsolute);
            return BitmapFrame.Create(oUri);
        }
        public static string ConvertToUnsecureString(this SecureString securePassword)
        {
            if (securePassword == null)
                throw new ArgumentNullException("securePassword");

            IntPtr unmanagedString = IntPtr.Zero;
            try
            {
                unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(securePassword);
                return Marshal.PtrToStringUni(unmanagedString);
            }
            finally
            {
                Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
            }
        }



        public static string Base64Decode(this string data)
        {
            try
            {
                ASCIIEncoding encoder = new ASCIIEncoding();
                Decoder asciiDecode = encoder.GetDecoder();

                byte[] todecodeByte = Convert.FromBase64String(data);
                int charCount = asciiDecode.GetCharCount(todecodeByte, 0, todecodeByte.Length);
                char[] decodedChar = new char[charCount];
                asciiDecode.GetChars(todecodeByte, 0, todecodeByte.Length, decodedChar, 0);
                string result = new String(decodedChar);
                return result;
            }
            catch (Exception e)
            {
                throw new Exception("Error in base64Decode" + e.Message);
            }
        } 

        public static bool LoadRawFile(string path, out byte[] rawData)
        {
            try
            {
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                long fileSize = fs.Length;

                rawData = new byte[fileSize];
                fs.Read(rawData, 0, Convert.ToInt32(fileSize));
                fs.Close();
                return true;
            }
            catch (Exception x)
            {
                rawData = null;
                ErrorHandler.ThrowError(new Exception("Fehler beim Laden des Bildes", x), "", false);
                return false;
            }
        }
    }

}
